#ifndef YCBASEUPLOADER_H
#define YCBASEUPLOADER_H
#include "base/task/task.h"
#include "request.h"
#include <QFile>
#include <QNetworkReply>
#include <QPointer>

namespace yc {

class Service;

struct UploadResource
{
    QString name;
    qint64 size;
    QString url;
};

class Uploader : public Task
{
    Q_OBJECT

public:
    Uploader(Service* service, const QString& service_url, const QString& local_dir_path,
             const QList<UploadResource>& resources, QObject* parent);
    Uploader(Service* service, const QString& service_url, const QString& local_dir_path,
             const QList<UploadResource>& resources, int id = 0, bool auto_delete = true,
             int timeout = 0, int try_times = 1, QObject* parent = nullptr);
    ~Uploader();

    void setHeaders(const RequestHeaders& headers);
    qint64 evaluateTotalMSecs() const override;

Q_SIGNALS:
    void progress(qint64 bytes_sent, qint64 bytes_total);
    void uploaded(const QString& id, const QString& url, const QString& tag);

private:
    void startImpl() override;
    void terminateImpl() override;
    void onTimeout() override;
    void uploadFile();
    void startNextRequest();
    void closeFile();

    QPointer<Service> service_;
    const QString service_url_;
    const QString local_dir_path_;
    const QList<UploadResource> resources_;
    QPointer<QNetworkReply> reply_;
    QFile* file_;
    qint64 bytes_total_;
    qint64 bytes_sent_;
    int pos_;
    RequestHeaders headers_;
    bool has_timeout_option_;
    int try_times_;
    int current_try_times_;
};

} // namespace yc

#endif // YCBASEUPLOADER_H
